﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace GiaoDien
{
    public partial class FormMuonSach : Form
    {
        private DataTable docgia;
        private DataTable muonsach;
        private DataTable sach;
        private int dem;
       // private string sql;

        public FormMuonSach()
        {
            InitializeComponent();
        }

        private void init()
        {
            docgia = new DataTable();
            sach = new DataTable();
         //   sql = "";
        }

        private void Loadmasach()
        {
            if (sach.Rows.Count != 0)
            {
                AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
                tbmasach.AutoCompleteMode = AutoCompleteMode.Append;
                tbmasach.AutoCompleteSource = AutoCompleteSource.CustomSource;
                IDataReader read = sach.CreateDataReader();
                while (read.Read())
                {
                    auto.Add(read["MaSach"].ToString());
                }
                tbmasach.AutoCompleteCustomSource = auto;
            }
        }

        private void Loadsach()
        {
            SACHBUS sBUS = new SACHBUS();
            sach = sBUS.Bangsach();

        }

        private void Loadmadocgia()
        {
            if (docgia.Rows.Count != 0)
            {
                AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
                tbmadocgia.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                tbmadocgia.AutoCompleteSource = AutoCompleteSource.CustomSource;
                IDataReader read = docgia.CreateDataReader();
                while (read.Read())
                {
                    auto.Add(read["MaDocGia"].ToString());
                }
                tbmadocgia.AutoCompleteCustomSource = auto;
            }
        }

        private void loaddocgia()
        {
            DOCGIABUS dgBUS = new DOCGIABUS();
            docgia = dgBUS.Loaddocgia();
        }

        private void Loadngaymuon()
        {
            tbngaymuon.Text = DateTime.Now.ToShortDateString();
            tbngaytra.Text = DateTime.Now.AddDays(3).ToShortDateString();
        }

        private void FormMuonSach_Load(object sender, EventArgs e)
        {
            init();
            Loadsach();
            Loadmasach();
            loaddocgia();
            Loadmadocgia();
            Loadngaymuon();
            muonsach = createtable();
            themoi();
        }

        private void them(string masach)
        {
            DataRow row = muonsach.NewRow();
            DataView view = new DataView(sach);
            string filter = "MaSach  = '" + masach + "'";
            view.RowFilter = filter;
            row["MaSach"] = view.Table.Rows[0]["MaSach"];
            row["TenSach"] = view.Table.Rows[0]["TenSach"];

            row["TheLoai"] = view.Table.Rows[0]["MaTheLoai"];
            THELOAISACHBUS tlsBUS = new THELOAISACHBUS();
            row["TheLoai"] = tlsBUS.laytentheloai(row["TheLoai"].ToString());

            row["TacGia"] = view.Table.Rows[0]["MaTacGia"];
            TACGIABUS tgBUS = new TACGIABUS();
            row["TacGia"] = tgBUS.laytentacgia(Convert.ToInt16(row["TacGia"].ToString()));
            muonsach.Rows.Add(row);
        }

        private DataTable createtable()
        {
            DataTable datatable = new DataTable();
            DataColumn column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.Caption = "Mã Sách";
            column.ColumnName = "MaSach";
            datatable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.Caption = "Tên Sách";
            column.ColumnName = "TenSach";
            datatable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.Caption = "Thể Loại";
            column.ColumnName = "TheLoai";
            datatable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.Caption = "Tác Giả";
            column.ColumnName = "TacGia";
            datatable.Columns.Add(column);

            return datatable;
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            if (dem < 3)
            {
                string masach = tbmasach.Text.Trim().ToUpper();
                if (masach != "")
                {
                    them(masach);
                    this.dataGridView1.DataSource = muonsach;
                    dem++;
                    tbmasach.ResetText();
                }
            }
            else MessageBox.Show("Quá số lượng cho mượn");
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            DataGridViewRow crow = dataGridView1.CurrentRow;
            int thutu = 0;
            foreach (DataRow row in muonsach.Rows)
            {
                if (string.Compare(row[0].ToString(), crow.Cells[0].ToString()) == 1)
                {
                    break;
                }
                thutu++;
            }
            muonsach.Rows.RemoveAt(thutu);
            this.dataGridView1.DataSource = muonsach;
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnlapthe_Click(object sender, EventArgs e)
        {
            try
            {

                PHIEUMUONSACHBUS pmBUS = new PHIEUMUONSACHBUS();
                CHITIETPHIEUMUONSACHBUS ctpmBUS = new CHITIETPHIEUMUONSACHBUS();

                PHIEUMUONSACHDTO pmDTO = new PHIEUMUONSACHDTO();
                pmDTO.Madocgia = tbmadocgia.Text.Trim();
                pmDTO.Ngaymuon = Convert.ToDateTime(tbngaymuon.Text.Trim());
                pmDTO.Ngaytra = Convert.ToDateTime(tbngaytra.Text.Trim());

                int temp = pmBUS.lapphieumuonsach(pmDTO);

                CHITIETPHIEUMUONSACHDTO ctpmDTO = new CHITIETPHIEUMUONSACHDTO();

                //lay gia tri maphieumuon de gan vao machitietphieumuon
                BIENPHIEUMUONSACHBUS bpmBUS = new BIENPHIEUMUONSACHBUS();
                int co = bpmBUS.laybienphieumuonsachhientai();
                ctpmDTO.Machitietphieumuonsach = co;
                ctpmDTO.Tinhtrang = "0";
                foreach (DataRow row in muonsach.Rows)
                {
                    ctpmDTO.Masach = row["MaSach"].ToString();
                    ctpmDTO.Tensach = row["TenSach"].ToString();
                    ctpmBUS.lapchitietphieumuonsach(ctpmDTO);
                }
                MessageBox.Show("Tao thanh cong");
                themoi();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void themoi()
        {
            tbmadocgia.ResetText();
            tbtendocgia.ResetText();
            tbngaysinh.ResetText();
            tbemail.ResetText();
            tbdiachi.ResetText();
            tbdienthoai.ResetText();
            tbngaylapthe.ResetText();
            tbngayhethan.ResetText();
            tbloaidocgia.ResetText();
            tbngaymuon.ResetText();
            tbngaytra.ResetText();
            tbmasach.ResetText();
            DataTable data = new DataTable();
            this.dataGridView1.DataSource = data;
        }

        private void btnthemoi_Click(object sender, EventArgs e)
        {
            themoi();
        }

        private void tbmadocgia_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string madocgia = tbmadocgia.Text.Trim().ToUpper();
                PHIEUMUONSACHBUS pmBUS = new PHIEUMUONSACHBUS();
                int tem = pmBUS.kiemtradamuonchua(madocgia);
                if (tem > 0)
                {
                    MessageBox.Show("da muon sach roi");
                    themoi();
                    return;
                }
                else
                {

                    DOCGIABUS dgBUS = new DOCGIABUS();
                    DataTable temp = dgBUS.Loaddocgia(madocgia);
                    if (temp.Rows.Count != 0)
                    {
                        DataRow row = temp.Rows[0];

                        tbtendocgia.Text = row["HoTen"].ToString();
                        tbngaysinh.Text = row["NgaySinh"].ToString();
                        tbemail.Text = row["Email"].ToString();
                        tbdiachi.Text = row["DiaChi"].ToString();
                        tbdienthoai.Text = row["DienThoai"].ToString();
                        tbngaylapthe.Text = row["NgayLapThe"].ToString();
                        tbngayhethan.Text = row["NgayHetHan"].ToString();
                        tbloaidocgia.Text = row["MaLoaiDocGia"].ToString();
                    }
                    else
                    {
                        MessageBox.Show("khong tồn tại");
                    }
                }

            }
        }

    }
}
